
#import "DatePicker.h"

@implementation DatePicker
-(UIDatePicker*)datePicker
{
    return datePicker;
}
-(UIButton*)btnDate
{
    return btnDate;
}
-(NSDate*)date
{
    return date;
}
-(void)setDate:(NSDate *)d
{
    date=d;
    datePicker.date=date;
}
-(NSDateFormatter*)dateFormatter
{
    return dateFormatter;
}
-(void)setDateFormatter:(NSDateFormatter *)df{
    dateFormatter=df;
    [btnDate setTitle:[dateFormatter stringFromDate:date] forState:UIControlStateNormal];
}
-(UIDatePickerMode)datePickerMode
{
    return datePickerMode;
}
-(void)setDatePickerMode:(UIDatePickerMode)mode{
    datePickerMode=mode;
    datePicker.datePickerMode=datePickerMode;
}
- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        //默认日期格式为yyyy-MM-dd
        dateFormatter= [[NSDateFormatter alloc] init];
        [dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];//location设置为中国
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        //picker的默认时间为当前时间
        date=[NSDate date];
        //picker的默认style为只显示日期
        
        //构造一个子视图,用于显示日期选择器
        subview=[[UIView alloc]initWithFrame:CGRectMake(0, 20, 320, 460)];
        subview.backgroundColor=[UIColor clearColor];
        subview.tag=0;
        UIButton * btnClose = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 246)];
        [btnClose addTarget:self action:@selector(btnCloseClick) forControlEvents:(UIControlEventTouchUpInside)];
        btnClose.backgroundColor = [UIColor blackColor];
        btnClose.alpha=0.6;
        [subview addSubview:btnClose];
        //[btnClose release];
//        //为子视图构造工具栏按钮
//        UIBarButtonItem* item = [[UIBarButtonItem alloc]  
//                                  initWithBarButtonSystemItem:UIBarButtonSystemItemStop
//                                  target:self action:@selector(btnCloseClick)];
//        NSArray* buttons=[NSArray arrayWithObjects:item,nil];
//        //为子视图构造工具栏
//        UIToolbar *subToolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
//        subToolbar.barStyle = UIBarStyleBlackTranslucent; 
//        [subToolbar sizeToFit];
//        [subToolbar setItems:buttons animated:YES]; //把按钮加入工具栏
//        [subview addSubview:subToolbar];//把工具栏加入子视图
//        [subToolbar release];
        //为子视图构造datePicker
        datePicker=[[UIDatePicker alloc]init];
        [datePicker setDate:date];
        datePicker.frame=CGRectMake(0, 480-216-20, 320, 216);
        datePickerMode=UIDatePickerModeDate;
        datePicker.datePickerMode=datePickerMode;
        //指定datepicker的valueChanged事件
        [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
        [subview addSubview:datePicker]; //把datePicker加入子视图
        //上面是子视图，下面是父视图
        //文本框
        btnDate=[UIButton buttonWithType:UIButtonTypeCustom];
        btnDate.backgroundColor = [UIColor clearColor];
        [btnDate setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btnDate.frame=CGRectMake(0, 0, 80, 30);
        [self bringSubviewToFront:btnDate];
        [btnDate.titleLabel setFont:[UIFont systemFontOfSize:14]]; 
        [btnDate setTitle:[dateFormatter stringFromDate:date] forState:UIControlStateNormal]; 
        [self addSubview:btnDate];
        //[btnDate addTarget:self action:@selector(clickdate:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return self;
}
//当datepicker的值改变时触发
-(void)dateChanged:(id)sender{
    date = [sender date];//获取datepicker的日期
    //改变textField的值
    NSString* str = [NSString stringWithString:[dateFormatter stringFromDate:date]];
    [btnDate setTitle:str forState:UIControlStateNormal];
}
//关闭按钮点击时触发
-(void)btnCloseClick{
    if(subview!=nil){
        subview.tag=0;
        [subview removeFromSuperview];
    }
}

- (void)dealloc {
    [date release];
    [dateFormatter release];
    [datePicker release];
    [subview release];
    [super dealloc];
}
-(void)clickdate:(id)sender{
    if (subview.tag==0) {//若tag标志等于0，说明datepicker未显示
        //置tag标志为1，并显示子视图
        subview.tag=1;
        [self.window addSubview:subview];
    }
}
-(NSInteger)getSubViewTag{
    return subview.tag;
}
-(void)showSubView{
    subview.tag=1;
    [self.window addSubview:subview];
}


#pragma mark textField delegate method
////当textField被点击时触发
//-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
//    if (subview.tag==0) {//若tag标志等于0，说明datepicker未显示
//        //置tag标志为1，并显示子视图
//        subview.tag=1;
//        [self.superview addSubview:subview];
//    }
//    return NO;
//}

@end